package com.archstoday.activity;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.archstoday.R;
import com.archstoday.adapter.ViewPagerAdapter;
import com.archstoday.fragment.CommentFragment;
import com.base.utils.StringUtil;

/**
 * 用户评论记录页面
 */
public class CommentActivity extends AsBaseActivity {

    private TextView mTitleTv;
    private TextView mLeftBtn, mRightBtn;
    private View mLeftLine, mRightLine;
    private ViewPager mViewPager;
    private ViewPagerAdapter mPagerAdapter;

    @Override
    protected void initView(Bundle bundle) {
        setContentView(R.layout.activity_comment);
        mTitleTv = (TextView) findViewById(R.id.title_tv);
        mLeftBtn = (TextView) findViewById(R.id.comment_tab_left_btn);
        mRightBtn = (TextView) findViewById(R.id.comment_tab_right_btn);
        mLeftLine = findViewById(R.id.comment_tab_left_line);
        mRightLine = findViewById(R.id.comment_tab_right_line);
        mViewPager = (ViewPager) findViewById(R.id.comment_pager);
    }

    @Override
    protected void initData(Bundle bundle) {
        mPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
        mPagerAdapter.addFragment(CommentFragment.newInstance(0));
        mPagerAdapter.addFragment(CommentFragment.newInstance(1));
        mViewPager.setAdapter(mPagerAdapter);

        showTab(0);
        mViewPager.setCurrentItem(0);
    }

    @Override
    protected void initListener() {
        setOnClickListener(R.id.back_btn);
        setOnClickListener(R.id.comment_tab_left_btn);
        setOnClickListener(R.id.comment_tab_right_btn);

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                showTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void treatClickEvent(int id) {
        switch (id) {
            case R.id.back_btn:
                appFinish();
                break;
            case R.id.comment_tab_left_btn:
                showTab(0);
                mViewPager.setCurrentItem(0);
                break;
            case R.id.comment_tab_right_btn:
                showTab(1);
                mViewPager.setCurrentItem(1);
                break;
        }
    }

    private void showTab(int index) {
        switch (index) {
            case 0:
                mLeftBtn.setSelected(true);
                mRightBtn.setSelected(false);
                mLeftLine.setVisibility(View.VISIBLE);
                mRightLine.setVisibility(View.GONE);
                break;
            case 1:
                mLeftBtn.setSelected(false);
                mRightBtn.setSelected(true);
                mLeftLine.setVisibility(View.GONE);
                mRightLine.setVisibility(View.VISIBLE);
                break;
        }
    }

    public void setTitleTv(int num) {
        mTitleTv.setText(StringUtil.replace(getString(R.string.user_comment_title),
                "*", String.valueOf(num)));
    }
}